Geolocation based Speedometer and Compass


Posted by wayne201299 on 2023-10-03

DEMO

取得目前移動速度,以及指北針

實作

  1. 同上篇,要取得瀏覽器定位功能所以要先啟動本地server
  2. 監聽Web API 的地理位置變化事件

     const arrow = document.querySelector(".arrow");
     const speed = document.querySelector(".speed-value");
    
     navigator.geolocation.watchPosition(
         (position) => {
             speed.textContent = position.coords.speed;
             arrow.style.transform = `rotate(${position.coords.heading}deg)`;
         },
         (err) => {
             console.log(err);
             alert("Geolocation is not supported by this browser.");
         }
     );
    

總結

  • 了解如何透過navigator.geolocation取得目前地理位址

#javascript







Related Posts

F2E合作社|分頁導覽列|Bootstrap 5網頁框架開發入門

F2E合作社|分頁導覽列|Bootstrap 5網頁框架開發入門

Day03 - CSS基本視覺格式

Day03 - CSS基本視覺格式

讀書筆記-前端三十1: HTML, CSS, JS

讀書筆記-前端三十1: HTML, CSS, JS


Comments